package com.podium.comment.utils;

import com.podium.auth.utils.ServletUtils;
import jakarta.servlet.http.HttpServletRequest;

/**
 * @author 邵文泽
 * @description 请求工具类
 * @date 2025/5/12 9:34
 */
public class RequestUtils {


    /**
     * 获取请求IP地址
     *
     * @return IP地址
     */
    public static String getIpAddress() {
        return ServletUtils.getClientIp();
    }

    /**
     * 获取设备信息
     *
     * @return 设备信息
     */
    public static String getDeviceInfo() {
        HttpServletRequest request = ServletUtils.getRequest();
        if (request == null) {
            return null;
        }
        String userAgent = request.getHeader("User-Agent");
        if (userAgent != null) {
            // 示例：Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
            String[] parts = userAgent.split(" ");
            if (parts.length >= 2) {
                return parts[1].replace("(", "").replace(")", "");
            }
        }
        return null;
    }

    /**
     * 获取User-Agent
     *
     * @return User-Agent
     */
    public static String  getUserAgent(){
        HttpServletRequest request = ServletUtils.getRequest();
        if (request == null) {
            return null;
        }
        return request.getHeader("User-Agent");
    }
}
